﻿Ext.define('app.system.user.EditPass',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    width: 600,
    height: 400,
    layout: 'fit',
    userId: 0,
    bodyStyle: 'padding:30px',
    initComponent: function () {
        var me = this;
        me.items = [me.getForm()];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            App.AjaxPost('/api/services/app/user/GetUserForEdit', { Id: _loginUserId }, function (data) {
                console.log(data.result);
                me.getForm().getForm().setValues(data.result.user);
            });
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                defaults: { anchor: '100%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        xtype: 'hidden',
                        name: 'id'
                    },
                    {
                        fieldLabel: '账号',
                        name: 'userName',
                        disabled:true
                    },
                    {
                        fieldLabel: '姓名',
                        name: 'name'
                    },
                    {
                        fieldLabel: '邮箱',
                        allowBlank: false,
                        name: 'emailAddress'
                    },
                    {
                        xtype: 'textfield',
                        fieldLabel: '原密码',
                        inputType: 'password',
                        //id: 'txt_oldPass',
                        name: 'oldPass'
                    },
                    {
                        xtype: 'textfield',
                        fieldLabel: '新密码',
                        //id: 'txt_firstPass',
                        inputType: 'password',
                        name: 'newPass'
                    },
                    {
                        xtype: 'textfield',
                        fieldLabel: '确认密码',
                       // id: 'txt_secondPass',
                        inputType: 'password',
                        name: 'confirmPass'
                    }
                ]
            });
        }
        return this.form;
    },
    onSubmit: function () {
        var me = this;
        var form = me.getForm().getForm();

        if (!form.isValid()) {
            return false;
        }
        var json = form.getValues();
        var txt_oldPass = json.oldPass;
        if (txt_oldPass != "")
        {
            var txt_firstPass = json.newPass;
            var txt_secondPass = json.confirmPass;
            if (txt_firstPass == "" || txt_secondPass == "") {
                Ext.Msg.alert("系统提示", "密码不能为空");
                return false;
            }
            if (txt_firstPass != txt_secondPass) {
                Ext.Msg.alert("系统提示", "两次密码输入不一致");
                return false;
            }
        } else {
            Ext.Msg.alert("系统提示", "密码不能为空");
            return false;
        }
        var valJson = form.getValues();
        App.AjaxPost('/api/services/app/user/ChangeUserInfo', {
            Id: valJson.id,
            Name: valJson.name,
            EmailAddress: valJson.emailAddress,
            OldPassword: valJson.oldPass,
            NewPassword: valJson.newPass
        }, function (data) {
            me.fireEvent("savesuccess");
        });
    }
});